#pragma once

#include <iostream>
#include <string>
#include <functional>
#include <cstring>
#include <strings.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#include "Protocol.hpp"
#include "Util.hpp"

namespace server
{
    enum
    {
        USAGE_ERR = 1,
        SOCK_ERR,
        BIND_ERR,
        LISTEN_ERR
    };

    static const uint16_t gport = 8080;
    static const int gbacklog = 5;

    using func_t = function<bool(HttpRequest &, HttpResponse &)>;

    class HttpServer
    {
    public:
        HttpServer(const func_t& func,const uint16_t &port = gport)
            : _func(func),_listensock(-1), _port(port)
        {
        }

        void initServer()
        {
            // 1. 创建socket文件套接字对象
            _listensock = socket(AF_INET, SOCK_STREAM, 0);
            if (_listensock < 0)
            {
                exit(SOCK_ERR);
            }

            // 2. bind绑定自己的网络信息
            struct sockaddr_in local;
            memset(&local, 0, sizeof local);

            local.sin_family = AF_INET;
            local.sin_port = htons(_port);
            local.sin_addr.s_addr = INADDR_ANY;

            int n = bind(_listensock, (struct sockaddr *)&local, sizeof local);
            if (n < 0)
            {
                exit(BIND_ERR);
            }

            // 3. 设置socket 为监听状态
            n = listen(_listensock, gbacklog);
            if (n < 0)
            {
                exit(LISTEN_ERR);
            }
        }

        void HandlerHttp(int sock)
        {
            // 1. 读到完整的http请求
            // 2. 反序列化
            // 3. httprequst, httpresponse, _func(req, resp)
            // 4. resp序列化
            // 5. send
            char buffer[4096];
            HttpRequest req;
            HttpResponse resp;
            size_t n = recv(sock, buffer, sizeof(buffer) - 1, 0);
            if (n > 0)
            {
                buffer[n] = 0;
                req.inbuffer = buffer;
                req.prase();

                _func(req, resp);
                send(sock,resp.outbuffer.c_str(),resp.outbuffer.size(),0);
            }
        }

        void start()
        {
            for (;;)
            {
                // 4. server 获取新链接
                struct sockaddr_in peer;
                bzero(&peer, sizeof peer);
                socklen_t len = sizeof(peer);

                int sock = accept(_listensock, (struct sockaddr *)&peer, &len);
                if (sock < 0)
                {
                    continue;
                }

                pid_t id = fork();
                if (id == 0)
                {
                    close(_listensock);
                    if (fork() > 0)
                        exit(0);
                    HandlerHttp(sock);
                    close(sock);
                    exit(0);
                }

                close(sock);
                waitpid(id, nullptr, 0);
            }
        }
        ~HttpServer()
        {
        }

    private:
        int _listensock;
        uint16_t _port;
        func_t _func;
    };
}